﻿using Pay.IO;
using SharpYaml.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Pay.Configuration
{
   public static  class Config
    {
        private static IDictionary<string, object> _configs = new Dictionary<string, object>();

        private static string _configPath = string.Empty;

        private static bool _isPublish = false;

        private static string _configFileMd5 = string.Empty;

        /// <summary>
        /// 配置文件初始化
        /// </summary>
        /// <param name="filename">配置文件绝对路径</param>
        /// <param name="isPublish">是否为发布版本,开发后期会选择为配置文件加密</param>
        public static void Init(string filename, bool isPublish = false)
        {
            if (!string.IsNullOrWhiteSpace(_configPath))
                throw new Exception("Config已初始化");
            _configPath = filename;
            _isPublish = isPublish;
            _configFileMd5 = LotFile.GetFileMD5(_configPath);
            Reload();

            ThreadPool.QueueUserWorkItem((c) =>
            {
                while (true)
                {
                    if (LotFile.GetFileMD5(_configPath) != _configFileMd5)
                    {
                        Reload();
                    }
                    Thread.Sleep(1000 * 60);
                }
            });
        }

        /// <summary>
        /// 重新载入配置文件
        /// </summary>
        public static void Reload()
        {
            if (string.IsNullOrWhiteSpace(_configPath))
                throw new Exception("Config未初始化");

            _configs.Clear();
            var configContent = LotFile.ReadAllText(_configPath);
            var serializer = new Serializer();
            var roots = serializer.Deserialize<Dictionary<string, Dictionary<string, dynamic>>>(configContent);

            var rootKey = string.Empty;
            foreach (var root in roots)
            {
                rootKey = root.Key;
                _configs.Add(rootKey, root.Value);
                foreach (var item in root.Value)
                {
                    _configs.Add($"{rootKey}:{item.Key}", item.Value);
                }
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键,区分大小写</param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            return GetValue<string>(key);
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">键,区分大小写</param>
        /// <returns></returns>
        public static T GetValue<T>(string key)
        {

            if (_configs.ContainsKey(key))
            {

                return (T)Convert.ChangeType(_configs[key], typeof(T));
            }
            return default(T);
        }

        /// <summary>
        /// 获取集合
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IList<string> GetList(string key)
        {
            return GetList<string>(key);
        }

        /// <summary>
        /// 获取集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IList<T> GetList<T>(string key)
        {
            if (_configs.ContainsKey(key))
            {
                var list = (_configs[key] as List<object>);
                var output = list.ConvertAll(input => (T)Convert.ChangeType(input, typeof(T)));
                return output;
            }
            return null;
        }
    }
}
